package com.sphchina.esb.webservice;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.gson.JsonObject;
import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.NcPaybillDetailMapper;
import com.javaweb.admin.mapper.NcPaybillMapper;
import com.javaweb.admin.service.IC37InfCkScBillNewService;
import com.javaweb.admin.service.IC37InfRkScBillNewService;
import com.javaweb.admin.service.INcServiceLogService;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.DynamicDataSource;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.seegw.shaphar.gspreport.NCReportData;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Service;

import com.sphchina.esb.webservice.DefaultWebServiceRequest;
import com.sphchina.esb.webservice.DefaultWebServiceResponse;
import com.sphchina.esb.webservice.ISoapWebServiceServiceSoapBindingStub;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@org.springframework.stereotype.Service
public class SendToReviewImpl implements ISendToReviewLog {

	/**
	 * ��־��¼
	 */
	@Autowired
	INcServiceLogService ncServiceLogService;

	@Autowired
	IC37InfCkScBillNewService c37InfCkScBillNewService;

	@Autowired
	IC37InfRkScBillNewService c37InfRkScBillNewService;

	@Autowired
	NcPaybillMapper ncPaybillMapper;
	@Autowired
	private NcPaybillDetailMapper ncPaybillDetailMapper;

	@Override
	public JsonResult callEsb(String body,String ServiceId) {
		String info = "";
		String endPoint= CommonConfig.ncServiceUrlKdl;
		if(endPoint==null){
			JsonResult.error("NC WebService服务地址未配置，请检查");
		}
		ISoapWebServiceServiceSoapBindingStub esb ;
		DefaultWebServiceResponse resp = null;
		Date dt=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DefaultWebServiceRequest  df=new DefaultWebServiceRequest();
		df.setMsgBody(body);
		df.setMsgSendTime(format.format(dt));
		df.setServiceId(ServiceId);
		df.setSourceAppCode("E9");//Դϵͳ��־��
		NCDto dto = JSON.parseObject(body,NCDto.class);
		NcServiceLog log = new NcServiceLog();
		//String danj_no = dto.body.length>0?dto.body[0].getDanj_no():"";
		log.setSubmitNo(ServiceId);
		log.setRequestBody(df.getMsgBody());
		log.setSubmitTime(DateUtils.now());
		try {
			esb = new ISoapWebServiceServiceSoapBindingStub(new URL(endPoint), new Service());
			resp = esb.receiveMessage(df);
//			info=resp.getReturnMessage().replaceAll("\\\\","");
			info=resp.getReturnMessage();
			JSONObject result = JSON.parseObject(info);
			if(Boolean.valueOf(result.getString("flag"))==true)
			{
				//log.setResponseBody(info);
				//log.setResponseCode(result.getString("flag"));
				//log.setResponseResult(result.getString("msg"));

				//log.setResponseCode("N");
				//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
				//ncServiceLogService.save(log);
				//c37InfCkScBillNewService.UpdateZT(danj_no,"Y");
				NcPaybill entity = new NcPaybill();
				UpdateWrapper<NcPaybill>  updateWrapper = new UpdateWrapper<>();
				updateWrapper.set("status","1").eq("id",ServiceId);
				ncPaybillMapper.update(entity,updateWrapper);
				//DynamicDataSource.clearDataSource();
			}
			else
			{
				log.setResponseBody(info);
				log.setResponseCode(result.getString("flag"));
				log.setResponseResult(result.getString("msg"));
				ncServiceLogService.save(log);
				return JsonResult.error(result.getString("msg"));

			}

		} catch (AxisFault e) {
			e.printStackTrace();
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			return JsonResult.error(e.getMessage()+"Return:"+info);
		} catch (Exception e) {
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			e.printStackTrace();
			return JsonResult.error(e.getMessage()+"Return:"+info);
		}

		return JsonResult.success(info);
	}

	@Override
	public JsonResult callEsbKdl(String billType,String billno,String body) {
		String info = "";
		String endPoint= CommonConfig.ncServiceUrlKdl;
		if(endPoint==null){
			JsonResult.error("NC WebService服务地址未配置，请检查");
		}
		ISoapWebServiceServiceSoapBindingStub esb ;
		DefaultWebServiceResponse resp = null;
		Date dt=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DefaultWebServiceRequest  df=new DefaultWebServiceRequest();
		df.setMsgBody(body);
		df.setMsgSendTime(format.format(dt));
		df.setServiceId("E0BO01");
		df.setSourceAppCode("E0");//Դϵͳ��־��
		//NCDto dto = JSON.parseObject(body,NCDto.class);
		NcServiceLog log = new NcServiceLog();
		//String danj_no = dto.body.length>0?dto.body[0].getDanj_no():"";
		log.setSubmitNo(billno+"_"+billType);
		log.setRequestBody(df.getMsgBody());
		log.setSubmitTime(DateUtils.now());
		String datasource= DynamicDataSource.getDataSource();
		DynamicDataSource.setDataSource("system");
		try {
			esb = new ISoapWebServiceServiceSoapBindingStub(new URL(endPoint), new Service());
			resp = esb.receiveMessage(df);
//			info=resp.getReturnMessage().replaceAll("\\\\","");
			info=resp.getReturnMessage();
			JSONObject result = JSON.parseObject(info);
			log.setResponseBody(info);
			log.setResponseCode(result.getString("result"));
			log.setResponseResult(result.getString("msg"));
			ncServiceLogService.save(log);
			if("Y".equals(result.getString("result"))){
				return JsonResult.success();
			}else{
				return JsonResult.error(result.getString("msg"));
			}

		} catch (AxisFault e) {
			e.printStackTrace();
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			return JsonResult.error(e.getMessage()+"Return:"+info);
		} catch (Exception e) {
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			e.printStackTrace();
			return JsonResult.error(e.getMessage()+"Return:"+info);
		}
		finally {
			DynamicDataSource.setDataSource(datasource);
		}

	}


	@Override
	public JsonResult callNCFK(Integer[] ids, String ServiceId) {
		List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("status","0").in("id",ids));
		NCDto dto = new NCDto();
		List<NcPaybillDetail> detailList;
		StringBuilder sb = new StringBuilder();
		PaybillData billdata = new PaybillData();
		List<Items> listItems = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			BeanUtils.copyProperties(list.get(i),billdata);
			billdata.setPk_balatype(list.get(i).getPkBalatype());
			billdata.setPk_busitype(list.get(i).getPkBusitype());
			billdata.setPk_currtype(list.get(i).getPkCurrtype());
			billdata.setPk_deptid(list.get(i).getPkDeptid());
			billdata.setPk_group(list.get(i).getPkGroup());
			billdata.setPk_org(list.get(i).getPkOrg());
			billdata.setPk_psndoc(list.get(i).getPkPsndoc());
			billdata.setPk_tradetype(list.get(i).getPkTradetype());
			billdata.setCreationtime("");

			detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid",list.get(i).getId()));
			listItems.clear();
			for (int j = 0; j < detailList.size(); j++) {
				Items items = new Items();
				items.setCashitem("");
				items.setPayaccount(detailList.get(j).getPayaccount());
				items.setRecaccount(detailList.get(j).getRecaccount());
				items.setDef20(detailList.get(j).getDef20());
				items.setDefitem12("");
				items.setDefitem23("");
				items.setDefitem26("");
				items.setDefitem27("");
				items.setDefitem28("");
				items.setDefitem30("");
				items.setLocal_money_de(detailList.get(j).getLocalMoneyDe());
				items.setLocal_tax_de(detailList.get(j).getLocalTaxDe());
				items.setMoney_de(detailList.get(j).getMoneyDe());
				items.setNotax_de("0.00");
				items.setObjtype(detailList.get(j).getObjtype());
				items.setPk_deptid(detailList.get(j).getPkDeptid());
				items.setPk_psndoc(detailList.get(j).getPkPsndoc());
				items.setRowno(String.valueOf(detailList.get(j).getId()));
				items.setScomment(detailList.get(j).getScomment());
				items.setSupplier(detailList.get(j).getSupplier());
				items.setSzxmid("");
				items.setTaxrate(detailList.get(j).getTaxrate());


				listItems.add(items);

			}
			billdata.setItems(listItems);
			dto.setData(billdata);
			JsonResult result = this.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
			if(result.getCode()!=JsonResult.SUCCESS)
			{
				sb.append("付款单号:"+billdata.getBillno()+"导入NC失败:"+result.getMsg());
				continue;
			}

		}
		return JsonResult.success(sb.toString());
	}

	@Override
	public JsonResult callNCYF(Integer[] ids, String ServiceId) {
		List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("status", "0").in("id", ids));
		NCDto dto = new NCDto();
		List<NcPaybillDetail> detailList;
		StringBuilder sb = new StringBuilder();
		PaybillData billdata = new PaybillData();
		List<Items> listItems = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			dto = new NCDto();
			detailList = new ArrayList<>();
			listItems = new ArrayList<>();
			BeanUtils.copyProperties(list.get(i), billdata);
			billdata.setPk_balatype(list.get(i).getPkBalatype());
			billdata.setPk_billtype("");
			billdata.setPk_busitype(list.get(i).getPkBusitype());
			billdata.setPk_currtype(list.get(i).getPkCurrtype());
			billdata.setPk_deptid(list.get(i).getPkDeptid());
			billdata.setPk_group(list.get(i).getPkGroup());
			billdata.setPk_org(list.get(i).getPkOrg());
			billdata.setPk_psndoc(list.get(i).getPkPsndoc());
			billdata.setPk_tradetype("");
			billdata.setPk_tradetypeid(list.get(i).getPkTradetype());
			billdata.setLocal_money(list.get(i).getLocalMoney());
			billdata.setCreationtime("");

			detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid", list.get(i).getId()));
			listItems.clear();
			for (int j = 0; j < detailList.size(); j++) {
				Items items = new Items();
				items.setBillclass("yf");
				items.setPayaccount(detailList.get(j).getPayaccount());
				items.setRecaccount(detailList.get(j).getRecaccount());
				items.setCreationtime(detailList.get(i).getCreationtime());
				items.setCreator(detailList.get(i).getCreator());
				items.setDef20(detailList.get(j).getDef20());
				items.setDef24(detailList.get(j).getDef24());
				items.setDef25(detailList.get(j).getDef25());
				items.setDef28(detailList.get(j).getDef28());
				items.setDef30(detailList.get(j).getDef30());
				items.setFkyt(billdata.getFkyt());
				items.setCaltaxmny(detailList.get(j).getCaltaxmny());
				items.setLocal_money_cr(detailList.get(j).getLocalMoneyDe());
				items.setLocal_tax_cr(detailList.get(j).getLocalTaxCr());
				items.setMoney_bal(detailList.get(j).getLocalMoneyDe());
				items.setObjtype(detailList.get(j).getObjtype());
				items.setPk_deptid(detailList.get(j).getPkDeptid());
				items.setPk_psndoc(detailList.get(j).getPkPsndoc());
				items.setRowno(String.valueOf(detailList.get(j).getRowno()));
				items.setScomment(detailList.get(j).getScomment());
				items.setSupplier(detailList.get(j).getSupplier());
				items.setSzxmid("");
				items.setTaxrate(detailList.get(j).getTaxrate());


				listItems.add(items);

			}
			billdata.setItems(listItems);
			dto.setData(billdata);
			JsonResult result = this.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
			if (result.getCode() != JsonResult.SUCCESS) {
				sb.append("付款单号:" + billdata.getBillno() + "导入NC失败:" + result.getMsg());
				continue;
			}
		}
		return JsonResult.success(sb.toString());
	}

	@Override
	public JsonResult sendFKOrder() {
		List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("status","0").eq("billclass","fk"));
		NCDto dto = new NCDto();
		List<NcPaybillDetail> detailList;
		StringBuilder sb = new StringBuilder();
		PaybillData billdata = new PaybillData();
		List<Items> listItems = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			BeanUtils.copyProperties(list.get(i),billdata);
			billdata.setPk_balatype(list.get(i).getPkBalatype());
			billdata.setPk_busitype(list.get(i).getPkBusitype());
			billdata.setPk_currtype(list.get(i).getPkCurrtype());
			billdata.setPk_deptid(list.get(i).getPkDeptid());
			billdata.setPk_group(list.get(i).getPkGroup());
			billdata.setPk_org(list.get(i).getPkOrg());
			billdata.setPk_psndoc(list.get(i).getPkPsndoc());
			billdata.setPk_tradetype(list.get(i).getPkTradetype());
			billdata.setCreationtime("");

			detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid",list.get(i).getId()));
			listItems.clear();
			for (int j = 0; j < detailList.size(); j++) {
				Items items = new Items();
				items.setCashitem("");
				items.setPayaccount(detailList.get(j).getPayaccount());
				items.setRecaccount(detailList.get(j).getRecaccount());
				items.setDef20(detailList.get(j).getDef20());
				items.setDefitem12("");
				items.setDefitem23("");
				items.setDefitem26("");
				items.setDefitem27("");
				items.setDefitem28("");
				items.setDefitem30("");
				items.setLocal_money_de(detailList.get(j).getLocalMoneyDe());
				items.setLocal_tax_de(detailList.get(j).getLocalTaxDe());
				items.setMoney_de(detailList.get(j).getMoneyDe());
				items.setNotax_de("0.00");
				items.setObjtype(detailList.get(j).getObjtype());
				items.setPk_deptid(detailList.get(j).getPkDeptid());
				items.setPk_psndoc(detailList.get(j).getPkPsndoc());
				items.setRowno(String.valueOf(detailList.get(j).getId()));
				items.setScomment(detailList.get(j).getScomment());
				items.setSupplier(detailList.get(j).getSupplier());
				items.setSzxmid("");
				items.setTaxrate(detailList.get(j).getTaxrate());


				listItems.add(items);

			}
			billdata.setItems(listItems);
			dto.setData(billdata);
			JsonResult result = this.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
			if(result.getCode()!=JsonResult.SUCCESS)
			{
				sb.append("付款单号:"+billdata.getBillno()+"导入NC失败:"+result.getMsg());
				continue;
			}

		}
		return JsonResult.success(sb.toString());
	}

	@Override
	public JsonResult sendYFOrder() {
		List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("status", "0").eq("billclass", "yf"));
		NCDto dto = new NCDto();
		List<NcPaybillDetail> detailList;
		StringBuilder sb = new StringBuilder();
		PaybillData billdata = new PaybillData();
		List<Items> listItems = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			dto = new NCDto();
			detailList = new ArrayList<>();
			listItems = new ArrayList<>();
			BeanUtils.copyProperties(list.get(i), billdata);
			billdata.setPk_balatype(list.get(i).getPkBalatype());
			billdata.setPk_billtype("");
			billdata.setPk_busitype(list.get(i).getPkBusitype());
			billdata.setPk_currtype(list.get(i).getPkCurrtype());
			billdata.setPk_deptid(list.get(i).getPkDeptid());
			billdata.setPk_group(list.get(i).getPkGroup());
			billdata.setPk_org(list.get(i).getPkOrg());
			billdata.setPk_psndoc(list.get(i).getPkPsndoc());
			billdata.setPk_tradetype("");
			billdata.setPk_tradetypeid(list.get(i).getPkTradetype());
			billdata.setLocal_money(list.get(i).getLocalMoney());
			billdata.setCreationtime("");

			detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid", list.get(i).getId()));
			listItems.clear();
			for (int j = 0; j < detailList.size(); j++) {
				Items items = new Items();
				items.setBillclass("yf");
				items.setPayaccount(detailList.get(j).getPayaccount());
				items.setRecaccount(detailList.get(j).getRecaccount());
				items.setCreationtime(detailList.get(i).getCreationtime());
				items.setCreator(detailList.get(i).getCreator());
				items.setDef20(detailList.get(j).getDef20());
				items.setDef24(detailList.get(j).getDef24());
				items.setDef25(detailList.get(j).getDef25());
				items.setDef28(detailList.get(j).getDef28());
				items.setDef30(detailList.get(j).getDef30());
				items.setFkyt(billdata.getFkyt());
				items.setCaltaxmny(detailList.get(j).getCaltaxmny());
				items.setLocal_money_cr(detailList.get(j).getLocalMoneyDe());
				items.setLocal_tax_cr(detailList.get(j).getLocalTaxCr());
				items.setMoney_bal(detailList.get(j).getLocalMoneyDe());
				items.setObjtype(detailList.get(j).getObjtype());
				items.setPk_deptid(detailList.get(j).getPkDeptid());
				items.setPk_psndoc(detailList.get(j).getPkPsndoc());
				items.setRowno(String.valueOf(detailList.get(j).getRowno()));
				items.setScomment(detailList.get(j).getScomment());
				items.setSupplier(detailList.get(j).getSupplier());
				items.setSzxmid("");
				items.setTaxrate(detailList.get(j).getTaxrate());


				listItems.add(items);

			}
			billdata.setItems(listItems);
			dto.setData(billdata);
			JsonResult result = this.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
			if (result.getCode() != JsonResult.SUCCESS) {
				sb.append("付款单号:" + billdata.getBillno() + "导入NC失败:" + result.getMsg());
				continue;
			}
		}
		return JsonResult.success(sb.toString());
	}

	@Override
	public JsonResult sendFKOrderById(String id) {
		List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("id",id).eq("status","0").eq("billclass","fk"));
		NCDto dto = new NCDto();
		List<NcPaybillDetail> detailList;
		StringBuilder sb = new StringBuilder();
		PaybillData billdata = new PaybillData();
		List<Items> listItems = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			BeanUtils.copyProperties(list.get(i),billdata);
			billdata.setPk_balatype(list.get(i).getPkBalatype());
			billdata.setPk_busitype(list.get(i).getPkBusitype());
			billdata.setPk_currtype(list.get(i).getPkCurrtype());
			billdata.setPk_deptid(list.get(i).getPkDeptid());
			billdata.setPk_group(list.get(i).getPkGroup());
			billdata.setPk_org(list.get(i).getPkOrg());
			billdata.setPk_psndoc(list.get(i).getPkPsndoc());
			billdata.setPk_tradetype(list.get(i).getPkTradetype());
			billdata.setCreationtime(list.get(i).getCreationtime());

			detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid",list.get(i).getId()));
			listItems.clear();
			for (int j = 0; j < detailList.size(); j++) {
				Items items = new Items();
				items.setCashitem("");
				items.setPayaccount(detailList.get(j).getPayaccount());
				items.setRecaccount(detailList.get(j).getRecaccount());
				items.setDef20(detailList.get(j).getDef20());
				items.setDefitem12("");
				items.setDefitem23("");
				items.setDefitem26("");
				items.setDefitem27("");
				items.setDefitem28("");
				items.setDefitem30("");
				items.setLocal_money_de(detailList.get(j).getLocalMoneyDe());
				items.setLocal_tax_de(detailList.get(j).getLocalTaxDe());
				items.setMoney_de(detailList.get(j).getMoneyDe());
				items.setNotax_de("0.00");
				items.setObjtype(detailList.get(j).getObjtype());
				items.setPk_deptid(detailList.get(j).getPkDeptid());
				items.setPk_psndoc(detailList.get(j).getPkPsndoc());
				items.setRowno(String.valueOf(detailList.get(j).getId()));
				items.setScomment(detailList.get(j).getScomment());
				items.setSupplier(detailList.get(j).getSupplier());
				items.setSzxmid("");
				items.setTaxrate(detailList.get(j).getTaxrate());


				listItems.add(items);

			}
			billdata.setItems(listItems);
			dto.setData(billdata);
			JsonResult result = this.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
			if(result.getCode()!=JsonResult.SUCCESS)
			{
				sb.append("付款单号:"+billdata.getBillno()+"导入NC失败:"+result.getMsg());
				continue;
			}

		}
		return StringUtils.isEmpty(sb.toString())?JsonResult.success(sb.toString()):JsonResult.error(sb.toString());
	}

	@Override
	public JsonResult sendYFOrderById(String id) {
		List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("id",id).eq("status", "0").eq("billclass", "yf"));
		NCDto dto = new NCDto();
		List<NcPaybillDetail> detailList;
		StringBuilder sb = new StringBuilder();
		PaybillData billdata = new PaybillData();
		List<Items> listItems = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			dto = new NCDto();
			detailList = new ArrayList<>();
			listItems = new ArrayList<>();
			BeanUtils.copyProperties(list.get(i), billdata);
			billdata.setPk_balatype(list.get(i).getPkBalatype());
			billdata.setPk_billtype("");
			billdata.setPk_busitype(list.get(i).getPkBusitype());
			billdata.setPk_currtype(list.get(i).getPkCurrtype());
			billdata.setPk_deptid(list.get(i).getPkDeptid());
			billdata.setPk_group(list.get(i).getPkGroup());
			billdata.setPk_org(list.get(i).getPkOrg());
			billdata.setPk_psndoc(list.get(i).getPkPsndoc());
			billdata.setPk_tradetype("");
			billdata.setPk_tradetypeid(list.get(i).getPkTradetype());
			billdata.setLocal_money(list.get(i).getLocalMoney());
			billdata.setCreationtime(list.get(i).getCreationtime());

			detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid", list.get(i).getId()));
			listItems.clear();
			for (int j = 0; j < detailList.size(); j++) {
				Items items = new Items();
				items.setBillclass("yf");
				items.setPayaccount(detailList.get(j).getPayaccount());
				items.setRecaccount(detailList.get(j).getRecaccount());
				items.setCreationtime(detailList.get(i).getCreationtime());
				items.setCreator(detailList.get(i).getCreator());
				items.setDef20(detailList.get(j).getDef20());
				items.setDef24(detailList.get(j).getDef24());
				items.setDef25(detailList.get(j).getDef25());
				items.setDef28(detailList.get(j).getDef28());
				items.setDef30(detailList.get(j).getDef30());
				items.setFkyt(billdata.getFkyt());
				items.setCaltaxmny(detailList.get(j).getCaltaxmny());
				items.setLocal_money_cr(detailList.get(j).getLocalMoneyDe());
				items.setLocal_tax_cr(detailList.get(j).getLocalTaxCr());
				items.setMoney_bal(detailList.get(j).getLocalMoneyDe());
				items.setObjtype(detailList.get(j).getObjtype());
				items.setPk_deptid(detailList.get(j).getPkDeptid());
				items.setPk_psndoc(detailList.get(j).getPkPsndoc());
				items.setRowno(String.valueOf(detailList.get(j).getRowno()));
				items.setScomment(detailList.get(j).getScomment());
				items.setSupplier(detailList.get(j).getSupplier());
				items.setSzxmid("");
				items.setTaxrate(detailList.get(j).getTaxrate());


				listItems.add(items);

			}
			billdata.setItems(listItems);
			dto.setData(billdata);
			JsonResult result = this.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
			if (result.getCode() != JsonResult.SUCCESS) {
				sb.append("付款单号:" + billdata.getBillno() + "导入NC失败:" + result.getMsg());
				continue;
			}
		}
		return StringUtils.isEmpty(sb.toString())?JsonResult.success(sb.toString()):JsonResult.error(sb.toString());
	}

	//ESB推送采购入库
	@Override
	public JsonResult callCGRK(String[] djhs, String ServiceId) {
		String info = "";

		String endPoint= CommonConfig.ncServiceUrl;
		if(endPoint==null){
			return JsonResult.error("NC WebSevice服务地址未配置，请检查");
		}
		if(djhs==null || djhs.length==0)
		{
			return JsonResult.error("缺少单据号参数");
		}
		String danj_no = djhs[0];
		//根据单据号获取中间表的采购入库明细
		List<C37InfRkScBillNew> detail = c37InfRkScBillNewService.DetailList(danj_no);
		ESBDto dto = new ESBDto();
		dto.orgcode = CommonConfig.ncOrgCode;
		dto.type="采购入库";
		POrderInfo[] item = new POrderInfo[detail.size()];
		int index = 0;
		for (C37InfRkScBillNew v: detail) {
			item[index] = new POrderInfo();
			item[index].setDanj_no(v.getDanjNo());
			item[index].setRiqi_char(v.getRiqiChar());
			item[index].setDanw_no(v.getDanwNo());
			item[index].setCaigou_staff(v.getCaigouStaff());
			item[index].setShouh_staff(v.getShouhStaff());
			item[index].setHanghao(v.getHanghao());
			item[index].setShangp_no(v.getShangpNo());
			item[index].setLot(v.getLot());
			item[index].setShengc_char(v.getShengcChar());
			item[index].setYoux_char(v.getYouxChar());
			item[index].setRuk_type(v.getRukType());
			item[index].setJih_num(v.getJihNum());
			item[index].setNum(v.getNum());
			item[index].setPrice(v.getPrice());
			item[index].setRuk_no(v.getRukNo());
			item[index].setHangh_cgd(v.getHanghCgd());
			item[index].setZhij_staff(v.getZhijStaff());
			item[index].setZt(v.getZt());
			item[index].setVdef1("1");
			item[index].setVdef2(v.getYansRlt());
			index++;
		}
		dto.body = item;
		String body = JSON.toJSONString(dto);
		ISoapWebServiceServiceSoapBindingStub esb ;
		DefaultWebServiceResponse resp = null;
		Date dt=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DefaultWebServiceRequest  df=new DefaultWebServiceRequest();
		df.setMsgBody(body);
		df.setMsgSendTime(format.format(dt));
		df.setServiceId(ServiceId);
		df.setSourceAppCode("E9");//Դϵͳ��־��

		NcServiceLog log = new NcServiceLog();
		log.setSubmitNo(danj_no+"_"+dto.type);
		log.setRequestBody(df.getMsgBody());
		log.setSubmitTime(DateUtils.now());
		try {
			esb = new ISoapWebServiceServiceSoapBindingStub(new URL(endPoint), new Service());
			resp = esb.receiveMessage(df);
//			info=resp.getReturnMessage().replaceAll("\\\\","");
			info=resp.getReturnMessage();
			JSONObject result = JSON.parseObject(info);
			log.setResponseBody(info);
			log.setResponseCode(result.getString("flag"));
			log.setResponseResult(result.getString("msg"));
			ncServiceLogService.save(log);

			if(result.getString("flag").equals("Y"))
			{

				//log.setResponseCode("N");
				//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
				c37InfRkScBillNewService.UpdateZT(danj_no,"N");
				return JsonResult.success(result.getString("msg"));
			}
			else
			{
				if(result.getString("msg").contains(CommonConfig.ncBillKey))
				{
					c37InfRkScBillNewService.UpdateZT(danj_no,"N");
					return JsonResult.error(result.getString("msg"));
				}
				else {
					//log.setResponseCode("N");
					//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
					return JsonResult.error(result.getString("msg"));
				}

			}

		} catch (AxisFault e) {
			e.printStackTrace();
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			return JsonResult.error(e.getMessage()+"Return:"+info);
		} catch (Exception e) {
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			e.printStackTrace();
			return JsonResult.error(e.getMessage()+"Return:"+info);
		}


	}

	//ESB推送采购退货
	@Override
	public JsonResult callGJTC(String[] djhs, String ServiceId) {
		String info = "";

		String endPoint= CommonConfig.ncServiceUrl;
		if(endPoint==null){
			return JsonResult.error("NC WebSevice服务地址未配置，请检查");
		}
		if(djhs==null || djhs.length==0)
		{
			return JsonResult.error("缺少单据号参数");
		}
		String danj_no = djhs[0];
		//根据单据号获取中间表的采购入库明细
		List<C37InfCkScBillNew> detail = c37InfCkScBillNewService.DetailList(danj_no);
		ESBDto dto = new ESBDto();
		dto.orgcode = CommonConfig.ncOrgCode;
		dto.type="购进退出";
		POrderInfo[] item = new POrderInfo[detail.size()];
		int index = 0;
		for (C37InfCkScBillNew v: detail) {
			item[index] = new POrderInfo();
			item[index].setDanj_no(v.getDanjNo());
			item[index].setChuk_no(v.getChukNo());
			item[index].setDanw_no(v.getDanwNo());
			item[index].setRiqi_char(v.getRiqiChar());
			item[index].setYew_staff(v.getYewStaff());
			if(StringUtils.isEmpty(v.getTihWay()))
			{
				item[index].setTih_way("4");
			}
			else {
				item[index].setTih_way(v.getTihWay());
			}
			item[index].setYew_type(v.getYewType());
			item[index].setDanjxc_side(v.getDanjxcSide());
			item[index].setHanghao(v.getHanghao());
			item[index].setShangp_no(v.getShangpNo());
			item[index].setJih_num(v.getJihNum());
			item[index].setNum(v.getNum());
			item[index].setLot(v.getLot());
			item[index].setShengc_char(v.getShengcChar());
			item[index].setYoux_char(v.getYouxChar());
			item[index].setChongh_flg(v.getChonghFlg());
			item[index].setTuih_reason(v.getTuihReason());
			item[index].setZt(v.getZt());
			item[index].setKub(v.getKub());
			item[index].setVdef1("1");
			item[index].setVdef2(v.getKub());
			index++;
		}
		dto.body = item;
		String body = JSON.toJSONString(dto);
		ISoapWebServiceServiceSoapBindingStub esb ;
		DefaultWebServiceResponse resp = null;
		Date dt=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DefaultWebServiceRequest  df=new DefaultWebServiceRequest();
		df.setMsgBody(body);
		df.setMsgSendTime(format.format(dt));
		df.setServiceId(ServiceId);
		df.setSourceAppCode("E9");//Դϵͳ��־��

		NcServiceLog log = new NcServiceLog();
		log.setSubmitNo(danj_no+"_"+dto.type);
		log.setRequestBody(df.getMsgBody());
		log.setSubmitTime(DateUtils.now());
		try {
			esb = new ISoapWebServiceServiceSoapBindingStub(new URL(endPoint), new Service());
			resp = esb.receiveMessage(df);
//			info=resp.getReturnMessage().replaceAll("\\\\","");
			info=resp.getReturnMessage();
			JSONObject result = JSON.parseObject(info);
			log.setResponseBody(info);
			log.setResponseCode(result.getString("flag"));
			log.setResponseResult(result.getString("msg"));
			ncServiceLogService.save(log);
			if(result.getString("flag").equals("Y"))
			{

				//log.setResponseCode("N");
				//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
				c37InfCkScBillNewService.UpdateZT(danj_no,"N");
				return JsonResult.success(result.getString("msg"));
			}
			else
			{
				if(result.getString("msg").contains(CommonConfig.ncBillKey))
				{
					c37InfCkScBillNewService.UpdateZT(danj_no,"N");
					return JsonResult.error(result.getString("msg"));
				}
				else {
					//log.setResponseCode("N");
					//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
					return JsonResult.error(result.getString("msg"));
				}

			}

		} catch (AxisFault e) {
			e.printStackTrace();
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			return JsonResult.error(e.getMessage()+"Return:"+info);
		} catch (Exception e) {
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			e.printStackTrace();
			return JsonResult.error(e.getMessage()+"Return:"+info);
		}
	}

	//ESB推送销售出库
	@Override
	public JsonResult callXSCK(String[] djhs, String ServiceId) {
		String info = "";

		String endPoint= CommonConfig.ncServiceUrl;
		if(endPoint==null){
			return JsonResult.error("NC WebSevice服务地址未配置，请检查");
		}
		if(djhs==null || djhs.length==0)
		{
			return JsonResult.error("缺少单据号参数");
		}
		String danj_no = djhs[0];
		//根据单据号获取中间表的采购入库明细
		List<C37InfCkScBillNew> detail = c37InfCkScBillNewService.DetailList(danj_no);
		ESBDto dto = new ESBDto();
		dto.orgcode = CommonConfig.ncOrgCode;
		dto.type="销售出库";
		POrderInfo[] item = new POrderInfo[detail.size()];
		int index = 0;
		for (C37InfCkScBillNew v: detail) {
			item[index] = new POrderInfo();
			item[index].setDanj_no(v.getDanjNo());
			item[index].setChuk_no(v.getChukNo());
			item[index].setDanw_no(v.getDanwNo());
			item[index].setRiqi_char(v.getRiqiChar());
			item[index].setYew_staff(v.getYewStaff());
			item[index].setTih_way(v.getTihWay());
			item[index].setYew_type(v.getYewType());
			item[index].setDanjxc_side(v.getDanjxcSide());
			item[index].setHanghao(v.getHanghao());
			item[index].setShangp_no(v.getShangpNo());
			item[index].setJih_num(v.getJihNum());
			item[index].setNum(v.getNum());
			item[index].setLot(v.getLot());
			item[index].setShengc_char(v.getShengcChar());
			item[index].setYoux_char(v.getYouxChar());
			item[index].setChongh_flg(v.getChonghFlg());
			if(StringUtils.isEmpty(v.getTuihReason()))
			{
				item[index].setTuih_reason(".");
			}
			else {
				item[index].setTuih_reason(v.getTuihReason());
			}
			item[index].setZt(v.getZt());
			item[index].setKub(v.getKub());
			item[index].setVdef1("1");
			item[index].setVdef2(v.getKub());
			index++;
		}
		dto.body = item;
		String body = JSON.toJSONString(dto);
		ISoapWebServiceServiceSoapBindingStub esb ;
		DefaultWebServiceResponse resp = null;
		Date dt=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DefaultWebServiceRequest  df=new DefaultWebServiceRequest();
		df.setMsgBody(body);
		df.setMsgSendTime(format.format(dt));
		df.setServiceId(ServiceId);
		df.setSourceAppCode("E9");//Դϵͳ��־��

		NcServiceLog log = new NcServiceLog();
		log.setSubmitNo(danj_no+"_"+dto.type);
		log.setRequestBody(df.getMsgBody());
		log.setSubmitTime(DateUtils.now());
		try {
			esb = new ISoapWebServiceServiceSoapBindingStub(new URL(endPoint), new Service());
			resp = esb.receiveMessage(df);
//			info=resp.getReturnMessage().replaceAll("\\\\","");
			info=resp.getReturnMessage();
			JSONObject result = JSON.parseObject(info);
			log.setResponseBody(info);
			log.setResponseCode(result.getString("flag"));
			log.setResponseResult(result.getString("msg"));
			ncServiceLogService.save(log);
			if(result.getString("flag").equals("Y"))
			{

				//log.setResponseCode("N");
				//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
				c37InfCkScBillNewService.UpdateZT(danj_no,"N");
				return JsonResult.success("",result.getString("msg"));
			}
			else
			{
				//若返回信息中包含[单据号重复]关键字,则更新状态为N
				if(result.getString("msg").contains(CommonConfig.ncBillKey))
				{
					c37InfCkScBillNewService.UpdateZT(danj_no,"N");
					return JsonResult.error(result.getString("msg"));
				}
				else {
					//log.setResponseCode("N");
					//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
					return JsonResult.error(result.getString("msg"));
				}

			}

		} catch (AxisFault e) {
			e.printStackTrace();
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			return JsonResult.error(e.getMessage()+"Return:"+info);
		} catch (Exception e) {
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			e.printStackTrace();
			return JsonResult.error(e.getMessage()+"Return:"+info);
		}
	}

	//ESB推送销售退回
	@Override
	public JsonResult callXSTH(String[] djhs, String ServiceId) {
		String info = "";

		String endPoint= CommonConfig.ncServiceUrl;
		if(endPoint==null){
			return JsonResult.error("NC WebSevice服务地址未配置，请检查");
		}
		if(djhs==null || djhs.length==0)
		{
			return JsonResult.error("缺少单据号参数");
		}
		String danj_no = djhs[0];
		//根据单据号获取中间表的采购入库明细
		List<C37InfRkScBillNew> detail = c37InfRkScBillNewService.DetailList(danj_no);
		ESBDto dto = new ESBDto();
		dto.orgcode = CommonConfig.ncOrgCode;
		dto.type="销售退回";
		POrderInfo[] item = new POrderInfo[detail.size()];
		int index = 0;
		for (C37InfRkScBillNew v: detail) {
			item[index] = new POrderInfo();
			item[index].setDanj_no(v.getDanjNo());
			item[index].setRiqi_char(v.getRiqiChar());
			item[index].setDanw_no(v.getDanwNo());
			item[index].setCaigou_staff(v.getCaigouStaff());
			item[index].setShouh_staff(v.getShouhStaff());
			item[index].setHanghao(v.getHanghao());
			item[index].setShangp_no(v.getShangpNo());
			item[index].setLot(v.getLot());
			item[index].setShengc_char(v.getShengcChar());
			item[index].setYoux_char(v.getYouxChar());
			item[index].setRuk_type(v.getRukType());
			item[index].setJih_num(v.getJihNum());
			item[index].setNum(v.getNum());
			item[index].setPrice(v.getPrice());
			item[index].setRuk_no(v.getRukNo());
			item[index].setHangh_cgd(v.getHanghCgd());
			item[index].setZhij_staff(v.getZhijStaff());
			item[index].setZt(v.getZt());
			item[index].setVdef1("1");
			item[index].setVdef2(v.getYansRlt());
			index++;
		}
		dto.body = item;
		String body = JSON.toJSONString(dto);
		ISoapWebServiceServiceSoapBindingStub esb ;
		DefaultWebServiceResponse resp = null;
		Date dt=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DefaultWebServiceRequest  df=new DefaultWebServiceRequest();
		df.setMsgBody(body);
		df.setMsgSendTime(format.format(dt));
		df.setServiceId(ServiceId);
		df.setSourceAppCode("E9");//Դϵͳ��־��

		NcServiceLog log = new NcServiceLog();
		log.setSubmitNo(danj_no+"_"+dto.type);
		log.setRequestBody(df.getMsgBody());
		log.setSubmitTime(DateUtils.now());
		try {
			esb = new ISoapWebServiceServiceSoapBindingStub(new URL(endPoint), new Service());
			resp = esb.receiveMessage(df);
//			info=resp.getReturnMessage().replaceAll("\\\\","");
			info=resp.getReturnMessage();
			JSONObject result = JSON.parseObject(info);
			log.setResponseBody(info);
			log.setResponseCode(result.getString("flag"));
			log.setResponseResult(result.getString("msg"));
			ncServiceLogService.save(log);
			if(result.getString("flag").equals("Y"))
			{

				//log.setResponseCode("N");
				//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
				c37InfRkScBillNewService.UpdateZT(danj_no,"N");
				return JsonResult.success("",result.getString("msg"));
			}
			else
			{
				if(result.getString("msg").contains(CommonConfig.ncBillKey))
				{
					c37InfRkScBillNewService.UpdateZT(danj_no,"N");
					return JsonResult.error(result.getString("msg"));
				}
				else {
					//log.setResponseCode("N");
					//log.setResponseResult("{flag=Y, msg=当前数据项PRICE为空;当前数据项vdef1为空;}");
					return JsonResult.error(result.getString("msg"));
				}

			}

		} catch (AxisFault e) {
			e.printStackTrace();
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			return JsonResult.error(e.getMessage()+"Return:"+info);
		} catch (Exception e) {
			log.setResponseBody(e.getMessage());
			log.setResponseCode("500");
			log.setResponseResult("");
			ncServiceLogService.save(log);
			e.printStackTrace();
			return JsonResult.error(e.getMessage()+"Return:"+info);
		}
	}
}
